package com.cfpl.admin_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Vegetable;
import com.cfpl.admin_service.service.VegetableService;
import com.cfpl.admin_service.mapper.VegetableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 14720
* @description 针对表【vegetable(果蔬表)】的数据库操作Service实现
* @createDate 2023-10-10 11:09:53
*/
@Service
public class VegetableServiceImpl extends ServiceImpl<VegetableMapper, Vegetable>
    implements VegetableService{

    @Autowired
    private VegetableMapper vegetableMapper;

    /**
     * 分页查询查询果蔬对象
     * @param currentPage 当前页
     * @param pageSize  每页条数
     * @param condition  查询条件 eg:根据果蔬名称进行查询
     * @return 果蔬对象
     */
    @Override
    public Page<Vegetable> queryPage(int currentPage, int pageSize, String condition) {
        LambdaQueryWrapper<Vegetable> queryWrapper = new LambdaQueryWrapper<>();
        if (condition != null) {
            queryWrapper.eq(Vegetable::getVegetableName,condition);
        }
        return vegetableMapper.selectPage(new Page<>(currentPage,pageSize),queryWrapper);
    }

    /**
     * 添加一条果蔬对象
     * @param vegetable 果蔬对象
     */
    @Override
    public void addVegetable(Vegetable vegetable) {
        vegetableMapper.insert(vegetable);
    }

    /**
     * 根据果蔬id进行具体查询
     * @param id 果蔬id
     * @return
     */
    @Override
    public Vegetable queryVegetableById(Long id) {
        return vegetableMapper.selectById(id);
    }

    /**
     * 根据果蔬ID删除果蔬对象
     * @param id 果蔬id
     */
    @Override
    public void removeVegetableById(Long id) {
        vegetableMapper.deleteById(id);
    }

    /**
     * 批量删除果蔬
     * @param Ids 果蔬对象ID列表
     */
    @Override
    public void deleteBatchByIds(List<Long> Ids) {
        if (Ids == null || Ids.size() < 1) {
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        vegetableMapper.deleteBatchIds(Ids);
    }

    /**
     * 更新果蔬对象
     * @param vegetable 新的果蔬对象
     */
    @Override
    public void updateVegetable(Vegetable vegetable) {
        vegetableMapper.updateById(vegetable);
    }
}




